﻿using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web.Mvc;
using UscSunShine.Models;
using PagedList;
using UscSunShine.ViewModels.ExtensionMethod;


namespace UscSunShine.Mvc.Controllers
{
    /// <summary>
    /// 公文管理
    /// </summary>
    public class PostController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        private const int pageSize = 10;

        /// <summary>
        /// 提供查询与分页
        /// </summary>
        /// <param name="departmentID">办事机构ID</param>
        /// <param name="postCategory">类别</param>
        /// <param name="titleSearchString">查询字</param>
        /// <param name="currentDepartmentID">当前部门ID</param>
        /// <param name="currentCategory">当前类别</param>
        /// <param name="currentTitle">当前查询字</param>
        /// <param name="page">分页页码</param>
        /// <returns></returns>
        public ActionResult Index(
            int? departmentID,
            PostCategory? postCategory,
            string titleSearchString,
            int? currentDepartmentID,
            PostCategory? currentCategory,
            string currentTitle,
            int? page)
        {
            //取数据
            var posts = db.Posts.Include(t => t.Department);
            //根据部门ID筛选
            if (departmentID != null)
            {
                posts = posts.Where(t => t.DepartmentID == departmentID);
                currentDepartmentID = departmentID;
            }
            else if (currentDepartmentID != null)
            {
                posts = posts.Where(t => t.DepartmentID == currentDepartmentID);
                departmentID = currentDepartmentID;
            }
            //根据类别筛选
            if (postCategory.HasValue)
            {
                posts = posts.Where(t => t.PostCategory == postCategory);
                currentCategory = postCategory;
            }
            else if (currentCategory.HasValue)
            {
                posts = posts.Where(t => t.PostCategory == currentCategory);
                postCategory = currentCategory;
            }
            //根据查询关键字筛选
            if (titleSearchString != null)
            {
                page = 1;
            }
            else
            {
                titleSearchString = currentTitle;
            }
            //保存当前状态
            ViewBag.currentTitle = titleSearchString;
            ViewBag.currentCategory = postCategory;
            ViewBag.currentDepartmentID = departmentID;

            ViewBag.departmentID = new SelectList(db.Departments, "ID", "Name", departmentID);
            if (postCategory.HasValue)
            {
                ViewBag.postCategory = new SelectList(EnumExtension.GetSelectList(typeof(PostCategory)), "Value", "Text", postCategory.Value);
            }
            else
            {
                ViewBag.postCategory = new SelectList(EnumExtension.GetSelectList(typeof(PostCategory)), "Value", "Text");
            }
            //根据查询关键字筛选数据
            if (!string.IsNullOrWhiteSpace(titleSearchString))
            {
                posts = posts.Where(t => t.Title.Contains(titleSearchString));
            }

            posts = posts.OrderByDescending(t => t.InputDate);

            var pageNumber = (page ?? 1);

            return View(posts.ToPagedList(pageNumber, pageSize));
        }


        // GET: Post/Details/5  
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Post post = await db.Posts.FindAsync(id);
            if (post == null)
            {
                return HttpNotFound();
            }
            //点击次数累加
            post.ViewTimes += 1;
            await db.SaveChangesAsync();

            return View(post);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
